<?php

/**
 * Ads form base class.
 *
 * @method Ads getObject() Returns the current form's model object
 *
 * @package    ndirou_affaire
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseAdsForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'               => new sfWidgetFormInputHidden(),
      'town_id'          => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Town'), 'add_empty' => false)),
      'category_id'      => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Category'), 'add_empty' => false)),
      'subcategory_id'   => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('SubCategory'), 'add_empty' => false)),
      'type_id'          => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Type'), 'add_empty' => false)),
      'seller_id'        => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Seller'), 'add_empty' => false)),
      'title'            => new sfWidgetFormInputText(),
      'description'      => new sfWidgetFormTextarea(),
      'price'            => new sfWidgetFormInputText(),
      'bonplan'          => new sfWidgetFormInputCheckbox(),
      'image_path1'      => new sfWidgetFormInputText(),
      'image_path2'      => new sfWidgetFormInputText(),
      'town'             => new sfWidgetFormInputText(),
      'metadescription'  => new sfWidgetFormInputText(),
      'metakeywords'     => new sfWidgetFormInputText(),
      'metatitle'        => new sfWidgetFormInputText(),
      'status_id'        => new sfWidgetFormInputCheckbox(),
      'is_prof'          => new sfWidgetFormInputCheckbox(),
      'created_at'       => new sfWidgetFormDateTime(),
      'updated_at'       => new sfWidgetFormDateTime(),
      'publish_start_at' => new sfWidgetFormDateTime(),
      'publish_end_at'   => new sfWidgetFormDateTime(),
      'slug'             => new sfWidgetFormInputText(),
      'adses_list'       => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Seller')),
    ));

    $this->setValidators(array(
      'id'               => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'town_id'          => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Town'))),
      'category_id'      => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Category'))),
      'subcategory_id'   => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('SubCategory'))),
      'type_id'          => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Type'))),
      'seller_id'        => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Seller'))),
      'title'            => new sfValidatorString(array('max_length' => 255)),
      'description'      => new sfValidatorString(array('required' => false)),
      'price'            => new sfValidatorInteger(),
      'bonplan'          => new sfValidatorBoolean(array('required' => false)),
      'image_path1'      => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'image_path2'      => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'town'             => new sfValidatorString(array('max_length' => 32, 'required' => false)),
      'metadescription'  => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'metakeywords'     => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'metatitle'        => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'status_id'        => new sfValidatorBoolean(array('required' => false)),
      'is_prof'          => new sfValidatorBoolean(array('required' => false)),
      'created_at'       => new sfValidatorDateTime(),
      'updated_at'       => new sfValidatorDateTime(),
      'publish_start_at' => new sfValidatorDateTime(array('required' => false)),
      'publish_end_at'   => new sfValidatorDateTime(array('required' => false)),
      'slug'             => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'adses_list'       => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Seller', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorDoctrineUnique(array('model' => 'Ads', 'column' => array('slug')))
    );

    $this->widgetSchema->setNameFormat('ads[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Ads';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['adses_list']))
    {
      $this->setDefault('adses_list', $this->object->Adses->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveAdsesList($con);

    parent::doSave($con);
  }

  public function saveAdsesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['adses_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Adses->getPrimaryKeys();
    $values = $this->getValue('adses_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Adses', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Adses', array_values($link));
    }
  }

}
